##################################################################
# Clean qualtrics data
##################################################################
#set working directory
setwd("~/Dropbox/Gender and Judicial Elections/JOP replication files")
#clear working environment
rm(list=ls())
#load packages
library(tidyverse) 
library(ggplot2)
library(stargazer)
library(readstata13)
library(splitstackshape)
library(lubridate)
library(lfe)
library(ggpubr)
library(xtable)
library(MASS)
library(reshape2)
library(openxlsx)
library(vtable)

########################################################################
# Survey Experiment Analysis
########################################################################
dat <- read.xlsx("./experiment_qualtricsdata.xlsx") %>% 
  slice(3:n()) %>%
  filter(Q3.1=="1,3") %>% filter(Q1.1!="") #or "Red,Green"
# filter to only those that passed the attention check and those that provided affirmative consent 

marybaileyrehab <- dat %>% filter(Q4.3!="") %>%
  mutate(candidate="female",platform="rehab",treatment="FR") %>%
  dplyr::select(ResponseId:Q4.14,Q13.1:treatment) %>% #Q4.3:Q4.14
  rename_all(.,funs(str_replace(.,"Q4.","question"))) 
michaelbaileyrehab <- dat %>% filter(Q5.3!="") %>%
  mutate(candidate="male",platform="rehab",treatment="MR") %>%
  dplyr::select(ResponseId:Q3.1,Q5.3:Q5.14,Q13.1:treatment) %>%  #Q5.3:Q5.14
  rename_all(.,funs(str_replace(.,"Q5.","question"))) 
michaelbaileytough <- dat %>% filter(Q6.3!="") %>%
  mutate(candidate="male",platform="tough",treatment="MT")  %>%
  dplyr::select(ResponseId:Q3.1,Q6.3:Q6.14,Q13.1:treatment) %>% #Q6.3:Q6.14
  rename_all(.,funs(str_replace(.,"Q6.","question")))  
marybaileytough <- dat %>% filter(Q7.3!="") %>%
  mutate(candidate="female",platform="tough",treatment="FT")  %>%
  dplyr::select(ResponseId:Q3.1,Q7.3:Q7.14,Q13.1:treatment) %>% #Q7.3:Q7.14
  rename_all(.,funs(str_replace(.,"Q7.","question")))  

clean <- rbind(marybaileyrehab,michaelbaileyrehab,michaelbaileytough,marybaileytough) %>%
  mutate_at(vars(question6:question14,Q13.1), as.numeric) %>% 
  mutate(question6 = dplyr::recode(question6, `1` = 4, `2` = 3,`3`=2,`4`=1),
         question7 = dplyr::recode(question7, `1` = 4, `2` = 3,`3`=2,`4`=1),
         question8 = dplyr::recode(question8, `1` = 4, `2` = 3,`3`=2,`4`=1),
         question12_1 = dplyr::recode(question12_1, `1` = 4, `2` = 3,`3`=2,`4`=1),
         question12_2 = dplyr::recode(question12_2, `1` = 4, `2` = 3,`3`=2,`4`=1),
         question12_3 = dplyr::recode(question12_3, `1` = 4, `2` = 3,`3`=2,`4`=1),
         question13 = dplyr::recode(question13, `1` = 4, `2` = 3,`3`=2,`4`=1),
         Q13.1 = dplyr::recode(Q13.1, `1` = 4, `2` = 3,`3`=2,`4`=1),
         Treatment=ifelse(treatment=="FR","Female Reform",
                          ifelse(treatment=="MR","Male Reform",
                                 ifelse(treatment=="FT","Female Tough","Male Tough"))),
         Gender=ifelse(candidate=="female","Female","Male"),
         Platform=ifelse(platform=="rehab","Reform","Tough")) 

clean <- rbind(marybaileyrehab,michaelbaileyrehab,michaelbaileytough,marybaileytough) %>%
  mutate_at(vars(question6:question14,Q13.1), as.numeric) %>% 
  mutate(question6 = dplyr::recode(question6, `1` = 4, `2` = 3,`3`=2,`4`=1),
         question7 = dplyr::recode(question7, `1` = 4, `2` = 3,`3`=2,`4`=1),
         question8 = dplyr::recode(question8, `1` = 4, `2` = 3,`3`=2,`4`=1),
         question12_1 = dplyr::recode(question12_1, `1` = 4, `2` = 3,`3`=2,`4`=1),
         question12_2 = dplyr::recode(question12_2, `1` = 4, `2` = 3,`3`=2,`4`=1),
         question12_3 = dplyr::recode(question12_3, `1` = 4, `2` = 3,`3`=2,`4`=1),
         question13 = dplyr::recode(question13, `1` = 4, `2` = 3,`3`=2,`4`=1),
         Q13.1 = dplyr::recode(Q13.1, `1` = 4, `2` = 3,`3`=2,`4`=1),
         Treatment=ifelse(treatment=="FR","Female Reform",
                          ifelse(treatment=="MR","Male Reform",
                                 ifelse(treatment=="FT","Female Tough","Male Tough"))),
         Gender=ifelse(candidate=="female","Female","Male"),
         Platform=ifelse(platform=="rehab","Reform","Tough"))

#######################################
# Table 17
#######################################
summary <- clean %>% dplyr::select(age:region,question6) %>%
  mutate(female=ifelse(gender==2,1,0),above60 = ifelse(as.numeric(hhi)<11,0,1),white=ifelse(ethnicity==1,1,0),
         associatesabove= ifelse(as.numeric(education)>4,1,0),democrat=ifelse(political_party<4,1,0),
         northeast=ifelse(region==1,1,0),midwest=ifelse(region==2,1,0),south=ifelse(region==3,1,0),west=ifelse(region==4,1,0)) %>%
  dplyr::select(female:west,question6)
stargazer(summary,covariate.labels = c("Female","Above 60,000 HHI",
                                       "White",
                                       "Associate's Degree or Above", "Democratic",
                                       "Northeast","Midwest","South","West","Support"),label="summarystats",digits=2,
          title="Summary Statistics of Lucid Sample",
          out="./tables/TableA17.tex")

#######################################
# Figure 2
#######################################
df.m <- clean %>% dplyr::select(treatment,age:ethnicity,education:political_party) %>% 
  melt(., "treatment") %>%
  filter(!is.na(value)) %>% mutate(value=as.numeric(value))

ggplot(data = df.m, aes(x=value)) + geom_density(aes(fill=treatment), alpha = 0.5) +
  facet_wrap(~variable,scales="free") +theme_bw()+
  scale_fill_grey(start = 0, end = .9)
ggsave("./figures/FigureA2.pdf")

#######################################
# Table 18
#######################################
mean_interact <- clean %>% dplyr::select(Treatment,question6) %>%
  group_by(Treatment) %>%
  dplyr::summarise(n=n(),`Mean Support`=mean(question6,na.rm=T))
print(xtable(mean_interact,label="mean_interact",caption="Comparison of means of rated support of the four treatments."),
      file="./tables/TableA18.tex",digits=4) 

#######################################
# Table 19
#######################################
female <- clean %>% filter(candidate=="female")
male <- clean %>% filter(candidate=="male")

t.vote <- t.test(female$question6,male$question6)
t.winning <- t.test(female$question7,male$question7)
t.impartial <- t.test(female$question8,male$question8)
t.people <- t.test(female$question10_1,male$question10_1)
t.yourinterests <- t.test(female$question10_2,male$question10_2)
t.safety <- t.test(female$question10_3,male$question10_3)
t.fairtrial <- t.test(female$question10_4,male$question10_4)
t.prop <- t.test(female$question12_1,male$question12_1)
t.viol <- t.test(female$question12_2,male$question12_2)
t.recid <- t.test(female$question12_3,male$question12_3)
t.incar <- t.test(female$question13,male$question13)

alloutcomes <- data.frame("Variable"=c("Support","Chance of Winning","Impartial Decisions","Represent Similar People",
                                       "Represent Your Interests","Ensure Safety","Receive Fair Trial",
                                       "Address Property Crime","Address Violent Crime","Address Recidivism",
                                       "Reduce Incarceration"),
                          "Female Candidate Mean"=c(mean(female$question6,na.rm=T),mean(female$question7,na.rm=T),mean(female$question8,na.rm=T),
                                                    mean(female$question10_1,na.rm=T),mean(female$question10_2,na.rm=T),
                                                    mean(female$question10_3,na.rm=T),mean(female$question10_4,na.rm=T),mean(female$question12_1,na.rm=T),
                                                    mean(female$question12_2,na.rm=T),mean(female$question12_3,na.rm=T),mean(female$question13,na.rm=T)),
                          "Male Candidate Mean"=c(mean(male$question6,na.rm=T),mean(male$question7,na.rm=T),mean(male$question8,na.rm=T),
                                                  mean(male$question10_1,na.rm=T),mean(male$question10_2,na.rm=T),
                                                  mean(male$question10_3,na.rm=T),mean(male$question10_4,na.rm=T),mean(male$question12_1,na.rm=T),
                                                  mean(male$question12_2,na.rm=T),mean(male$question12_3,na.rm=T),mean(male$question13,na.rm=T)),
                          "t statistic"=c(t.vote$statistic,t.winning$statistic,t.impartial$statistic,t.people$statistic,t.yourinterests$statistic,
                                          t.safety$statistic,t.fairtrial$statistic,t.prop$statistic,t.viol$statistic,
                                          t.recid$statistic,t.incar$statistic),
                          "p-value" = c(t.vote$p.value,t.winning$p.value,t.impartial$p.value,t.people$p.value,t.yourinterests$p.value,
                                        t.safety$p.value,t.fairtrial$p.value,t.prop$statistic,t.viol$p.value,
                                        t.recid$p.value,t.incar$p.value), check.names=FALSE)

print(xtable(alloutcomes,label="meanstable",caption="Comparison of means of rated support of the female and male judicial candidates and results from a series of two-sample t-tests."),
      file="./tables/TableA19.tex",digits=4) 

#######################################
# Table 20
#######################################
male1 <- clean %>% filter(Gender=="Male") %>% filter(gender==1) #male respondents, male treatment
male2 <- clean %>% filter(Gender=="Male") %>% filter(gender==2) #female respondents, male treatment
female1 <- clean %>% filter(Gender=="Female") %>% filter(gender==1) #male respondents, female treatment
female2 <- clean %>% filter(Gender=="Female") %>% filter(gender==2) #female respondents, female treatment
maletough1 <- clean %>% filter(Gender=="Male") %>% filter(platform=="tough") %>% 
  filter(gender==1)
maletough2 <- clean %>% filter(Gender=="Male") %>% filter(platform=="tough") %>% 
  filter(gender==2)
femaletough1 <- clean %>% filter(Gender=="Female")%>% filter(platform=="tough") %>% 
  filter(gender==1)
femaletough2 <- clean %>% filter(Gender=="Female")%>% filter(platform=="tough") %>% 
  filter(gender==2)
malereform1 <- clean %>% filter(Gender=="Male") %>% filter(platform=="rehab") %>% 
  filter(gender==1)
malereform2 <- clean %>% filter(Gender=="Male") %>% filter(platform=="rehab") %>% 
  filter(gender==2)
femalereform1 <- clean %>% filter(Gender=="Female")%>% filter(platform=="rehab") %>% 
  filter(gender==1)
femalereform2 <- clean %>% filter(Gender=="Female")%>% filter(platform=="rehab") %>% 
  filter(gender==2)

t.supportM <- t.test(male1$question6,male2$question6)
t.supportF <- t.test(female1$question6,female2$question6)
t.supportMT <- t.test(maletough1$question6,maletough2$question6)
t.supportFT <- t.test(femaletough1$question6,femaletough2$question6)
t.supportMR <- t.test(malereform1$question6,malereform2$question6)
t.supportFR <- t.test(femalereform1$question6,femalereform2$question6)

alloutcomes <- data.frame("Variable"=c("Male Candidate","Female Candidate","Male Tough Candidate","Female Tough Candidate",
                                       "Male Reform Candidate","Female Reform Candidate"),
                          `Support Mean Among Men`=c(mean(male1$question6,na.rm=T),mean(female1$question8,na.rm=T),
                                                     mean(maletough1$question10_2,na.rm=T),mean(femaletough1$question10_4,na.rm=T),
                                                     mean(malereform1$question12_2,na.rm=T),
                                                     mean(femalereform1$question13,na.rm=T)),
                          `Support Mean Among Women`=c(mean(male2$question7,na.rm=T),
                                                       mean(female2$question10_1,na.rm=T),
                                                       mean(maletough2$question10_3,na.rm=T),mean(femaletough2$question12_1,na.rm=T),
                                                       mean(malereform2$question12_3,na.rm=T),
                                                       mean(femalereform2$question13,na.rm=T)),
                          "t statistic"=c(t.supportM$statistic,t.supportF$statistic,t.supportMT$statistic,t.supportFT$statistic,t.supportMR$statistic,
                                          t.supportFR$statistic),
                          "p-value" = c(t.supportM$p.value,t.supportF$p.value,t.supportMT$p.value,t.supportFT$p.value,t.supportMR$p.value,
                                        t.supportFR$p.value),check.names = F)

print(xtable(alloutcomes,label="meanstableallsubsetsgender",caption="Comparison of means of rated support of judicial candidates by respondent gender and platform and results from a series of two-sample t-tests."),
      file="./tables/TableA20.tex", digits=4,size="\\scriptsize")

#######################################
# Figure 3
#######################################
my_comparisons <- list(c("Reform","Tough"))
ggplot(clean, aes(x=as.factor(Platform), y=question6)) + 
  geom_violin() + stat_summary(fun.y=mean, geom="point", shape=23, size=2) + theme_bw()+
  stat_summary(fun = mean, geom = "text", size=6,
               vjust = -1, aes(label = paste("Mean:", round(..y.., digits = 2))))+xlab("Platform")+
  stat_compare_means(method="t.test",size=6,label = "p.format",comparisons = my_comparisons)+
  ylab("Likelihood of Voting for Candidate")+scale_y_continuous(breaks=c(1,2,3,4),
                                                                labels=c("Very\n Unlikely","Somewhat\n Unlikely","Somewhat\n Likely", "Very\n Likely"))
ggsave("./figures/FigureA3.pdf",height=8,width=12)

#######################################
# Table 21
#######################################
reform <- clean %>% filter(platform=="rehab")
tough <- clean %>% filter(platform=="tough")

t.vote <- t.test(reform$question6,tough$question6)
t.winning <- t.test(reform$question7,tough$question7)
t.impartial <- t.test(reform$question8,tough$question8)
t.people <- t.test(reform$question10_1,tough$question10_1)
t.yourinterests <- t.test(reform$question10_2,tough$question10_2)
t.safety <- t.test(reform$question10_3,tough$question10_3)
t.fairtrial <- t.test(reform$question10_4,tough$question10_4)
t.prop <- t.test(reform$question12_1,tough$question12_1)
t.viol <- t.test(reform$question12_2,tough$question12_2)
t.recid <- t.test(reform$question12_3,tough$question12_3)
t.incar <- t.test(reform$question13,tough$question13)

alloutcomes <- data.frame("Variable"=c("Support","Chance of Winning","Impartial Decisions","Represent Similar People",
                                       "Represent Your Interests","Ensure Safety","Receive Fair Trial",
                                       "Address Property Crime","Address Violent Crime","Address Recidivism",
                                       "Reduce Incarceration"),
                          "Reform Mean"=c(mean(reform$question6,na.rm=T),mean(reform$question7,na.rm=T),mean(reform$question8,na.rm=T),
                                          mean(reform$question10_1,na.rm=T),mean(reform$question10_2,na.rm=T),
                                          mean(reform$question10_3,na.rm=T),mean(reform$question10_4,na.rm=T),mean(reform$question12_1,na.rm=T),
                                          mean(reform$question12_2,na.rm=T),mean(reform$question12_3,na.rm=T),mean(reform$question13,na.rm=T)),
                          "Tough Mean"=c(mean(tough$question6,na.rm=T),mean(tough$question7,na.rm=T),mean(tough$question8,na.rm=T),
                                         mean(tough$question10_1,na.rm=T),mean(tough$question10_2,na.rm=T),
                                         mean(tough$question10_3,na.rm=T),mean(tough$question10_4,na.rm=T),mean(tough$question12_1,na.rm=T),
                                         mean(tough$question12_2,na.rm=T),mean(tough$question12_3,na.rm=T),mean(tough$question13,na.rm=T)),
                          "t statistic"=c(t.vote$statistic,t.winning$statistic,t.impartial$statistic,t.people$statistic,t.yourinterests$statistic,
                                          t.safety$statistic,t.fairtrial$statistic,t.prop$statistic,t.viol$statistic,
                                          t.recid$statistic,t.incar$statistic),
                          "p-value" = c(t.vote$p.value,t.winning$p.value,t.impartial$p.value,t.people$p.value,t.yourinterests$p.value,
                                        t.safety$p.value,t.fairtrial$p.value,t.prop$statistic,t.viol$p.value,
                                        t.recid$p.value,t.incar$p.value), check.names=FALSE)

print(xtable(alloutcomes,label="meanstablebyplatform",caption="Comparison of means of rated support of the reform and tough judicial candidates and results from a series of two-sample t-tests."),
      file="./tables/TableA21.tex",digits=4, size="\\scriptsize") 

#######################################
# Table 22
#######################################
male <- clean %>% filter(gender==1) %>% filter(candidate=="female")
female <- clean %>% filter(gender==2) %>% filter(candidate=="female")

t.vote <- t.test(female$question6,male$question6)
t.winning <- t.test(female$question7,male$question7)
t.impartial <- t.test(female$question8,male$question8)
t.people <- t.test(female$question10_1,male$question10_1)
t.yourinterests <- t.test(female$question10_2,male$question10_2)
t.safety <- t.test(female$question10_3,male$question10_3)
t.fairtrial <- t.test(female$question10_4,male$question10_4)
t.prop <- t.test(female$question12_1,male$question12_1)
t.viol <- t.test(female$question12_2,male$question12_2)
t.recid <- t.test(female$question12_3,male$question12_3)
t.incar <- t.test(female$question13,male$question13)

alloutcomes <- data.frame("Variable"=c("Support","Chance of Winning","Impartial Decisions","Represent Similar People",
                                       "Represent Your Interests","Ensure Safety","Receive Fair Trial",
                                       "Address Property Crime","Address Violent Crime","Address Recidivism",
                                       "Reduce Incarceration"),
                          "Female Mean Among Women"=c(mean(female$question6,na.rm=T),mean(female$question7,na.rm=T),mean(female$question8,na.rm=T),
                                                      mean(female$question10_1,na.rm=T),mean(female$question10_2,na.rm=T),
                                                      mean(female$question10_3,na.rm=T),mean(female$question10_4,na.rm=T),mean(female$question12_1,na.rm=T),
                                                      mean(female$question12_2,na.rm=T),mean(female$question12_3,na.rm=T),mean(female$question13,na.rm=T)),
                          "Female Mean Among Men"=c(mean(male$question6,na.rm=T),mean(male$question7,na.rm=T),mean(male$question8,na.rm=T),
                                                    mean(male$question10_1,na.rm=T),mean(male$question10_2,na.rm=T),
                                                    mean(male$question10_3,na.rm=T),mean(male$question10_4,na.rm=T),mean(male$question12_1,na.rm=T),
                                                    mean(male$question12_2,na.rm=T),mean(male$question12_3,na.rm=T),mean(male$question13,na.rm=T)),
                          "t statistic"=c(t.vote$statistic,t.winning$statistic,t.impartial$statistic,t.people$statistic,t.yourinterests$statistic,
                                          t.safety$statistic,t.fairtrial$statistic,t.prop$statistic,t.viol$statistic,
                                          t.recid$statistic,t.incar$statistic),
                          "p-value" = c(t.vote$p.value,t.winning$p.value,t.impartial$p.value,t.people$p.value,t.yourinterests$p.value,
                                        t.safety$p.value,t.fairtrial$p.value,t.prop$statistic,t.viol$p.value,
                                        t.recid$p.value,t.incar$p.value), check.names=FALSE)

print(xtable(alloutcomes,label="meanstablebygender",caption="Comparison of means of rated support of the female judicial candidate by respondent gender and results from a series of two-sample t-tests."),
      file="./tables/TableA22.tex",digits=4, size="\\scriptsize") 

#######################################
# Table 23
#######################################
dem <- clean %>% filter(political_party==1 | political_party==2 | political_party==3 | political_party==6) %>% 
  filter(candidate=="female")
rep <- clean %>% filter(political_party==5 | political_party==8 | political_party==9 | political_party==10)  %>% 
  filter(candidate=="female")

t.vote <- t.test(dem$question6,rep$question6)
t.winning <- t.test(dem$question7,rep$question7)
t.impartial <- t.test(dem$question8,rep$question8)
t.people <- t.test(dem$question10_1,rep$question10_1)
t.yourinterests <- t.test(dem$question10_2,rep$question10_2)
t.safety <- t.test(dem$question10_3,rep$question10_3)
t.fairtrial <- t.test(dem$question10_4,rep$question10_4)
t.prop <- t.test(dem$question12_1,rep$question12_1)
t.viol <- t.test(dem$question12_2,rep$question12_2)
t.recid <- t.test(dem$question12_3,rep$question12_3)
t.incar <- t.test(dem$question13,rep$question13)

alloutcomes <- data.frame("Variable"=c("Support","Chance of Winning","Impartial Decisions","Represent Similar People",
                                       "Represent Your Interests","Ensure Safety","Receive Fair Trial",
                                       "Address Property Crime","Address Violent Crime","Address Recidivism",
                                       "Reduce Incarceration"),
                          "Female Mean Among Democrats"=c(mean(dem$question6,na.rm=T),mean(dem$question7,na.rm=T),mean(dem$question8,na.rm=T),
                                                          mean(dem$question10_1,na.rm=T),mean(dem$question10_2,na.rm=T),
                                                          mean(dem$question10_3,na.rm=T),mean(dem$question10_4,na.rm=T),mean(dem$question12_1,na.rm=T),
                                                          mean(dem$question12_2,na.rm=T),mean(dem$question12_3,na.rm=T),mean(dem$question13,na.rm=T)),
                          "Female Mean Among Republicans"=c(mean(rep$question6,na.rm=T),mean(rep$question7,na.rm=T),mean(rep$question8,na.rm=T),
                                                            mean(rep$question10_1,na.rm=T),mean(rep$question10_2,na.rm=T),
                                                            mean(rep$question10_3,na.rm=T),mean(rep$question10_4,na.rm=T),mean(rep$question12_1,na.rm=T),
                                                            mean(rep$question12_2,na.rm=T),mean(rep$question12_3,na.rm=T),mean(rep$question13,na.rm=T)),
                          "t statistic"=c(t.vote$statistic,t.winning$statistic,t.impartial$statistic,t.people$statistic,t.yourinterests$statistic,
                                          t.safety$statistic,t.fairtrial$statistic,t.prop$statistic,t.viol$statistic,
                                          t.recid$statistic,t.incar$statistic),
                          "p-value" = c(t.vote$p.value,t.winning$p.value,t.impartial$p.value,t.people$p.value,t.yourinterests$p.value,
                                        t.safety$p.value,t.fairtrial$p.value,t.prop$statistic,t.viol$p.value,
                                        t.recid$p.value,t.incar$p.value), check.names=FALSE)

print(xtable(alloutcomes,label="meanstablebyparty",caption="Comparison of means of rated support of the female judicial candidate by respondent party and results from a series of two-sample t-tests."),
      file="./tables/TableA23.tex",digits=4, size="\\scriptsize") 

#######################################
# Table 24
#######################################
highknowledge <- clean %>% filter(Q13.1==3 | Q13.1==4) %>% 
  filter(candidate=="female")
lowknowledge <- clean %>% filter(Q13.1==1 | Q13.1==2)  %>% 
  filter(candidate=="female")

t.vote <- t.test(highknowledge$question6,lowknowledge$question6)
t.winning <- t.test(highknowledge$question7,lowknowledge$question7)
t.impartial <- t.test(highknowledge$question8,lowknowledge$question8)
t.people <- t.test(highknowledge$question10_1,lowknowledge$question10_1)
t.yourinterests <- t.test(highknowledge$question10_2,lowknowledge$question10_2)
t.safety <- t.test(highknowledge$question10_3,lowknowledge$question10_3)
t.fairtrial <- t.test(highknowledge$question10_4,lowknowledge$question10_4)
t.prop <- t.test(highknowledge$question12_1,lowknowledge$question12_1)
t.viol <- t.test(highknowledge$question12_2,lowknowledge$question12_2)
t.recid <- t.test(highknowledge$question12_3,lowknowledge$question12_3)
t.incar <- t.test(highknowledge$question13,lowknowledge$question13)

alloutcomes <- data.frame("Variable"=c("Support","Chance of Winning","Impartial Decisions","Represent Similar People",
                                       "Represent Your Interests","Ensure Safety","Receive Fair Trial",
                                       "Address Property Crime","Address Violent Crime","Address Recidivism",
                                       "Reduce Incarceration"),
                          "Female Mean Among High Knowledge"=c(mean(highknowledge$question6,na.rm=T),mean(highknowledge$question7,na.rm=T),mean(highknowledge$question8,na.rm=T),
                                                               mean(highknowledge$question10_1,na.rm=T),mean(highknowledge$question10_2,na.rm=T),
                                                               mean(highknowledge$question10_3,na.rm=T),mean(highknowledge$question10_4,na.rm=T),mean(highknowledge$question12_1,na.rm=T),
                                                               mean(highknowledge$question12_2,na.rm=T),mean(highknowledge$question12_3,na.rm=T),mean(highknowledge$question13,na.rm=T)),
                          "Female Mean Among Low Knowledge"=c(mean(lowknowledge$question6,na.rm=T),mean(lowknowledge$question7,na.rm=T),mean(lowknowledge$question8,na.rm=T),
                                                              mean(lowknowledge$question10_1,na.rm=T),mean(lowknowledge$question10_2,na.rm=T),
                                                              mean(lowknowledge$question10_3,na.rm=T),mean(lowknowledge$question10_4,na.rm=T),mean(lowknowledge$question12_1,na.rm=T),
                                                              mean(lowknowledge$question12_2,na.rm=T),mean(lowknowledge$question12_3,na.rm=T),mean(lowknowledge$question13,na.rm=T)),
                          "t statistic"=c(t.vote$statistic,t.winning$statistic,t.impartial$statistic,t.people$statistic,t.yourinterests$statistic,
                                          t.safety$statistic,t.fairtrial$statistic,t.prop$statistic,t.viol$statistic,
                                          t.recid$statistic,t.incar$statistic),
                          "p-value" = c(t.vote$p.value,t.winning$p.value,t.impartial$p.value,t.people$p.value,t.yourinterests$p.value,
                                        t.safety$p.value,t.fairtrial$p.value,t.prop$statistic,t.viol$p.value,
                                        t.recid$p.value,t.incar$p.value), check.names=FALSE)

print(xtable(alloutcomes,label="meanstablebyknowledge",caption="Comparison of means of rated support of the female judicial candidate by respondent self-rated knowledge of trial courts and results from a series of two-sample t-tests."),
      file="./tables/TableA24.tex",digits=4, size="\\scriptsize") 

#######################################
# Table 25
#######################################
dem_rehab <- clean %>% filter(political_party==1 | political_party==2 | political_party==3 | political_party==6) %>% 
  filter(platform=="rehab")
rep_rehab <- clean %>% filter(political_party==5 | political_party==8 | political_party==9 | political_party==10) %>% 
  filter(platform=="rehab")

t.vote <- t.test(dem_rehab$question6,rep_rehab$question6)
t.winning <- t.test(dem_rehab$question7,rep_rehab$question7)
t.impartial <- t.test(dem_rehab$question8,rep_rehab$question8)
t.people <- t.test(dem_rehab$question10_1,rep_rehab$question10_1)
t.yourinterests <- t.test(dem_rehab$question10_2,rep_rehab$question10_2)
t.safety <- t.test(dem_rehab$question10_3,rep_rehab$question10_3)
t.fairtrial <- t.test(dem_rehab$question10_4,rep_rehab$question10_4)
t.prop <- t.test(dem_rehab$question12_1,rep_rehab$question12_1)
t.viol <- t.test(dem_rehab$question12_2,rep_rehab$question12_2)
t.recid <- t.test(dem_rehab$question12_3,rep_rehab$question12_3)
t.incar <- t.test(dem_rehab$question13,rep_rehab$question13)

alloutcomes <- data.frame("Variable"=c("Support","Chance of Winning","Impartial Decisions","Represent Similar People",
                                       "Represent Your Interests","Ensure Safety","Receive Fair Trial",
                                       "Address Property Crime","Address Violent Crime","Address Recidivism",
                                       "Reduce Incarceration"),
                          "Reform Mean Among Democrats"=c(mean(dem_rehab$question6,na.rm=T),mean(dem_rehab$question7,na.rm=T),mean(dem_rehab$question8,na.rm=T),
                                                          mean(dem_rehab$question10_1,na.rm=T),mean(dem_rehab$question10_2,na.rm=T),
                                                          mean(dem_rehab$question10_3,na.rm=T),mean(dem_rehab$question10_4,na.rm=T),mean(dem_rehab$question12_1,na.rm=T),
                                                          mean(dem_rehab$question12_2,na.rm=T),mean(dem_rehab$question12_3,na.rm=T),mean(dem_rehab$question13,na.rm=T)),
                          "Reform Mean Among Republicans"=c(mean(rep_rehab$question6,na.rm=T),mean(rep_rehab$question7,na.rm=T),mean(rep_rehab$question8,na.rm=T),
                                                            mean(rep_rehab$question10_1,na.rm=T),mean(rep_rehab$question10_2,na.rm=T),
                                                            mean(rep_rehab$question10_3,na.rm=T),mean(rep_rehab$question10_4,na.rm=T),mean(rep_rehab$question12_1,na.rm=T),
                                                            mean(rep_rehab$question12_2,na.rm=T),mean(rep_rehab$question12_3,na.rm=T),mean(rep_rehab$question13,na.rm=T)),
                          "t statistic"=c(t.vote$statistic,t.winning$statistic,t.impartial$statistic,t.people$statistic,t.yourinterests$statistic,
                                          t.safety$statistic,t.fairtrial$statistic,t.prop$statistic,t.viol$statistic,
                                          t.recid$statistic,t.incar$statistic),
                          "p-value" = c(t.vote$p.value,t.winning$p.value,t.impartial$p.value,t.people$p.value,t.yourinterests$p.value,
                                        t.safety$p.value,t.fairtrial$p.value,t.prop$statistic,t.viol$p.value,
                                        t.recid$p.value,t.incar$p.value), check.names=FALSE)

print(xtable(alloutcomes,label="meanstablebyparty_platformreform",caption="Comparison of means of rated support of the judicial candidates by respondent party and platform and results from a series of two-sample t-tests."),
      file="./tables/TableA25.tex",digits=4, size="\\scriptsize") 

#######################################
# Table 26
#######################################
dem_tough <- clean %>% filter(political_party==1 | political_party==2 | political_party==3 | political_party==6) %>% 
  filter(platform=="tough")
rep_tough <- clean %>% filter(political_party==5 | political_party==8 | political_party==9 | political_party==10) %>% 
  filter(platform=="tough")

t.vote <- t.test(dem_tough$question6,rep_tough$question6)
t.winning <- t.test(dem_tough$question7,rep_tough$question7)
t.impartial <- t.test(dem_tough$question8,rep_tough$question8)
t.people <- t.test(dem_tough$question10_1,rep_tough$question10_1)
t.yourinterests <- t.test(dem_tough$question10_2,rep_tough$question10_2)
t.safety <- t.test(dem_tough$question10_3,rep_tough$question10_3)
t.fairtrial <- t.test(dem_tough$question10_4,rep_tough$question10_4)
t.prop <- t.test(dem_tough$question12_1,rep_tough$question12_1)
t.viol <- t.test(dem_tough$question12_2,rep_tough$question12_2)
t.recid <- t.test(dem_tough$question12_3,rep_tough$question12_3)
t.incar <- t.test(dem_tough$question13,rep_tough$question13)

alloutcomes <- data.frame("Variable"=c("Support","Chance of Winning","Impartial Decisions","Represent Similar People",
                                       "Represent Your Interests","Ensure Safety","Receive Fair Trial",
                                       "Address Property Crime","Address Violent Crime","Address Recidivism",
                                       "Reduce Incarceration"),
                          "Tough Mean Among Democrats"=c(mean(dem_tough$question6,na.rm=T),mean(dem_tough$question7,na.rm=T),mean(dem_tough$question8,na.rm=T),
                                                         mean(dem_tough$question10_1,na.rm=T),mean(dem_tough$question10_2,na.rm=T),
                                                         mean(dem_tough$question10_3,na.rm=T),mean(dem_tough$question10_4,na.rm=T),mean(dem_tough$question12_1,na.rm=T),
                                                         mean(dem_tough$question12_2,na.rm=T),mean(dem_tough$question12_3,na.rm=T),mean(dem_tough$question13,na.rm=T)),
                          "Tough Mean Among Republicans"=c(mean(rep_tough$question6,na.rm=T),mean(rep_tough$question7,na.rm=T),mean(rep_tough$question8,na.rm=T),
                                                           mean(rep_tough$question10_1,na.rm=T),mean(rep_tough$question10_2,na.rm=T),
                                                           mean(rep_tough$question10_3,na.rm=T),mean(rep_tough$question10_4,na.rm=T),mean(rep_tough$question12_1,na.rm=T),
                                                           mean(rep_tough$question12_2,na.rm=T),mean(rep_tough$question12_3,na.rm=T),mean(rep_tough$question13,na.rm=T)),
                          "t statistic"=c(t.vote$statistic,t.winning$statistic,t.impartial$statistic,t.people$statistic,t.yourinterests$statistic,
                                          t.safety$statistic,t.fairtrial$statistic,t.prop$statistic,t.viol$statistic,
                                          t.recid$statistic,t.incar$statistic),
                          "p-value" = c(t.vote$p.value,t.winning$p.value,t.impartial$p.value,t.people$p.value,t.yourinterests$p.value,
                                        t.safety$p.value,t.fairtrial$p.value,t.prop$statistic,t.viol$p.value,
                                        t.recid$p.value,t.incar$p.value), check.names=FALSE)

print(xtable(alloutcomes,label="meanstablebyparty_platformtough",caption="Comparison of means of rated support of the judicial candidates by respondent party and platform and results from a series of two-sample t-tests."),
      file="./tables/TableA26.tex",digits=4, size="\\scriptsize") 


#######################################
# Figure 4
#######################################
library(scales)
clean <- clean %>% mutate(question6 = rescale(question6))

female.tough <- clean %>% filter(Treatment == "Female Tough")
male.tough <- clean %>% filter(Treatment == "Male Tough")
female.reform <- clean %>% filter(Treatment == "Female Reform")
male.reform <- clean %>% filter(Treatment == "Male Reform")

tough.test <- t.test(female.tough$question6, male.tough$question6, conf.level = 0.9)
reform.test <- t.test(female.reform$question6, male.reform$question6, conf.level = 0.9)

# smallest substantively meaningful effect? 
sd(clean$question6, na.rm = T)/2

ci.dat <- data.frame(diff = c(0.6867168 - 0.6895674 , 0.6715328 - 0.6470588 ), 
                     lower = c(tough.test$conf.int[1], reform.test$conf.int[1]),
                     upper = c(tough.test$conf.int[2], reform.test$conf.int[2]),
                     term = c("Tough", "Reform"))
linetypes <- c("")
ggplot(aes(y = term), data = ci.dat) + 
  geom_pointrange(aes(x = diff, xmin = lower, xmax = upper)) + 
  geom_vline(xintercept = sd(clean$question6, na.rm = T)/2, lty = 2) +
  geom_vline(xintercept = -sd(clean$question6, na.rm = T)/2, lty = 2) + 
  geom_vline(xintercept = -sd(clean$question6, na.rm = T), lty = 3) +
  geom_vline(xintercept = sd(clean$question6, na.rm = T), lty = 3) +
  theme_bw() + 
  labs(y = " ", x = "Male - Female", title = "Difference in Support by Candidate Gender") +
  scale_x_continuous(breaks = c(-0.5, -sd(clean$question6, na.rm = T), -sd(clean$question6, na.rm = T)/2, 0,  sd(clean$question6, na.rm = T)/2, sd(clean$question6, na.rm = T), 0.5),
                     labels = c("-0.5", "-0.218\n(-1 SD)", "-0.109\n(-1/2 SD)", "0",  "0.109\n(1/2 SD)", "0.218\n(1 SD)", "0.5"),
                     limits = c(-0.5, 0.5))
ggsave("./figures/FigureA4.pdf",height=8,width=12)
